<div class="layuimini-container layuimini-page-anim">
  <div class="layuimini-main">

    <fieldset class="table-search-fieldset">
      <legend>搜索信息</legend>
      <div style="margin: 10px 10px 10px 10px">
        <form class="layui-form layui-form-pane" action="">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">秒杀商品id</label>
              <div class="layui-input-inline">
                <input type="text" name="id" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">商品名称</label>
              <div class="layui-input-inline">
                <input type="text" name="goodsName" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn">
                <i class="layui-icon"></i> 搜 索
              </button>
            </div>
          </div>
        </form>
      </div>
    </fieldset>

    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
      </div>
    </script>

    <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    <script type="text/html" id="currentTableBar">
      <a id="editBtn-{{d.id}}" class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
      <a id="delBtn-{{d.id}}" class="layui-btn layui-btn-xs layui-btn-danger data-count-delete"
         lay-event="delete">删除</a>
    </script>

  </div>
</div>

<script type="text/html" id="imgtmp">
  <img src="{{ d.goodsImg }}">
</script>

<script type="text/html" id="createTime">
  {{#  if(d.createTime != null){ }}
  <div>{{ layui.util.toDateString(d.createTime, 'yyyy年MM月dd日 HH:mm:ss') }}</div>
  {{#  } else {  }}
  <div>{{ }}</div>
  {{#  } }}
</script>

<script type="text/html" id="updateTime">
  {{#  if(d.updateTime != null){ }}
  <div>{{ layui.util.toDateString(d.updateTime, 'yyyy年MM月dd日 HH:mm:ss') }}</div>
  {{#  } else {  }}
  <div>{{ }}</div>
  {{#  } }}
</script>

<script type="text/html" id="startTime">
  {{#  if(d.startTime != null){ }}
  <div>{{ layui.util.toDateString(d.startTime, 'yyyy年MM月dd日 HH:mm:ss') }}</div>
  {{#  } else {  }}
  <div>{{ }}</div>
  {{#  } }}
</script>

<script type="text/html" id="endTime">
  {{#  if(d.endTime != null){ }}
  <div>{{ layui.util.toDateString(d.endTime, 'yyyy年MM月dd日 HH:mm:ss') }}</div>
  {{#  } else {  }}
  <div>{{ }}</div>
  {{#  } }}
</script>

<script>
    layui.use(['form', 'table', 'miniPage', 'element'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;

        table.render({
            elem: '#currentTableId',
            url: 'seckillGoods/mySeckillGoods',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', title: '秒杀商品id', width: 120, sort: true},
                    {field: 'goodsName', title: '商品名称'},
                    {field: 'goodsPrice', title: '商品价格'},
                    {field: 'seckillPrice', title: '秒杀商品价格'},
                    {field: 'goodsImg', title: '商品图片', templet: "#imgtmp"},
                    {field: 'goodsTitle', title: '商品标题'},
                    {field: 'stockCount', title: '秒杀商品库存', minWidth: 150, sort: true},
                    {field: 'goodsDetail', title: '商品详情'},
                    {field: 'createTime', title: '秒杀商品创建时间', templet: "#createTime"},
                    {field: 'updateTime', title: '秒杀商品最近修改时间', templet: "#updateTime"},
                    {field: 'startTime', title: '秒杀活动开始时间', templet: "#startTime"},
                    {field: 'endTime', title: '秒杀活动结束时间', templet: "#endTime"},
                    {title: '操作', minWidth: 100, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            // console.log(result)
            var index = layer.msg('查询中，请稍后。。。', {icon: 16, time: false, shade: 0});
            setTimeout(function () {
                table.reload('currentTableId', {
                    methods: "GET",
                    request: {
                        pageName: 'page', //页码的参数名称，默认：page
                        limitName: 'limit' //每页数据量的参数名，默认：limit
                    },
                    where: {
                        id: data.field.id,
                        goodsName: data.field.goodsName
                    },
                    // where: result,
                    page: {
                        curr: 1
                    }
                }, 'data');
                layer.close(index);
            }, 800);

            return false;
        });

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('table/seckillAdd.html');
                // var content = miniPage.getHrefContent('form.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '添加秒杀商品',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function () {
                        var index = layer.msg('查询中，请稍后。。。', {icon: 16, time: false, shade: 0});
                        setTimeout(function () {
                            table.reload('currentTableId', {
                                methods: "GET",
                                request: {
                                    pageName: 'page', //页码的参数名称，默认：page
                                    limitName: 'limit' //每页数据量的参数名，默认：limit
                                },
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                            layer.close(index);
                        }, 800);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });

            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId'), data = checkStatus.data, arr = " ";
                for (var i = 0; i < data.length; i++) {
                    arr = arr + " " + data[i].id;
                }

                layer.confirm("你确定要删除商品id为[ " + arr + " ]的商品吗？",
                    {btn: ["确定", "取消"]},
                    function () {
                        layui.use('miniPage', function () {
                            $.ajax({
                                url: "/goods/delGoods",
                                type: "DELETE",
                                contentType: 'application/json',
                                data: JSON.stringify(data),
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.msg(data.message)
                                    } else {
                                        layer.msg(data.message)
                                    }
                                },
                                error: function () {
                                    layer.msg("服务器异常！")
                                }
                            })
                        })
                    },
                    function (index) {
                        layer.close(index)
                    }
                )
                layer.close()
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            if (obj.event === 'edit') {
                // console.log(JSON.stringify(obj.data));

                var content = miniPage.getHrefContent('table/seckillEdit.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '编辑秒杀商品',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    success: function (layero, index1) {
                        var iframe = window['layui-layer-iframe' + index1];
                        //调用子页面的全局函数
                        child(obj.data)
                        console.log(JSON.stringify(obj.data))
                    },
                    end: function () {
                        var index = layer.msg('查询中，请稍后。。。', {icon: 16, time: false, shade: 0});
                        setTimeout(function () {
                            table.reload('currentTableId', {
                                methods: "GET",
                                request: {
                                    pageName: 'page', //页码的参数名称，默认：page
                                    limitName: 'limit' //每页数据量的参数名，默认：limit
                                },
                                page: {
                                    curr: 1
                                }
                            }, 'data');
                            layer.close(index);
                        }, 800);
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {

                layer.confirm("你确定要删除商品id为[ " + obj.data.id + " ]的商品吗？",
                    {btn: ["确定", "取消"]},
                    function () {
                        layui.use('miniPage', function () {
                            $.ajax({
                                url: "/seckillGoods/delGoods",
                                type: "DELETE",
                                contentType: 'application/json',
                                data: '[' + JSON.stringify(obj.data) + ']',
                                success: function (data) {
                                    if (data.code == 200) {
                                        obj.del();
                                        layer.msg(data.message)
                                    } else {
                                        layer.msg(data.message)
                                    }
                                },
                                error: function () {
                                    layer.msg("服务器异常！")
                                }
                            })
                        })
                    },
                    function () {
                        layer.close()
                    }
                )
                layer.close()
            }
        });
    });

    var sonData = null;

    function getSonValue(obj) {
        // console.log(obj)
        sonData = obj
    }
</script>